package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.network.entity.AtUserEntity;
import com.zw.yzk.kabu.data.network.entity.SearchUserEntity;
import com.zw.yzk.kabu.utils.CircleTransform;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/7/3.
 */

public class SearchUserAdapter extends RecyclerView.Adapter<SearchUserAdapter.Holder> {

    private Context context;
    private List<SearchUserEntity.User> dataList;

    public SearchUserAdapter(List<SearchUserEntity.User> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
    }

    public void refresh(List<SearchUserEntity.User> dataList) {
        this.dataList.clear();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    public void add(List<SearchUserEntity.User> list) {
        int start = dataList.size();
        if (list != null) {
            this.dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_search_user_result, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final SearchUserEntity.User model = dataList.get(position);
        holder.bind(context, model);
        setCheckBoxStatus(holder.concern, model.followState);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClicked(model);
                }
            }
        });

        holder.concern.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (dataList.get(holder.getAdapterPosition()).followState) {
                    case 0:
                    case 1:
                        dataList.get(holder.getAdapterPosition()).followState = holder.concern.isSelected() ? 0 : 1;
                        if (listener != null) {
                            listener.onConcernedBtnClicked(!holder.concern.isSelected(), model);
                        }
                        break;
                    case 2:
                    case 3:
                        dataList.get(holder.getAdapterPosition()).followState = holder.concern.isSelected() ? 2 : 3;
                        if (listener != null) {
                            listener.onConcernedBtnClicked(!holder.concern.isSelected(), model);
                        }
                        break;
                    default:
                        break;
                }
                notifyItemChanged(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.sign)
        TextView sign;
        @Bind(R.id.concern)
        TextView concern;


        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Context context, SearchUserEntity.User user) {
            Glide.with(context)
                    .load(user.headImg)
                    .transform(new CircleTransform(context))
                    .error(R.drawable.ic_default_head)
                    .into(image);
            name.setText(user.nickName);
            sign.setText(user.sign);

        }
    }

    private void setCheckBoxStatus(TextView checkBox, int followState) {
        // 0:未关注 1:已关注 2:已被对方关注 3:互粉
        if (followState == 1) {
            checkBox.setSelected(true);
            checkBox.setText(R.string.has_concerned);
        } else if (followState == 3) {
            checkBox.setSelected(true);
            checkBox.setText(R.string.concerned_each_other);
        } else {
            checkBox.setSelected(false);
            checkBox.setText(R.string.concern);
        }
    }


    public interface OnItemClickedListener {
        void onItemClicked(SearchUserEntity.User user);

        void onConcernedBtnClicked(Boolean concern, SearchUserEntity.User user);
    }

    public OnItemClickedListener listener;

    public void setOnItemClickedListener(OnItemClickedListener listener) {
        this.listener = listener;
    }
}
